﻿using System;
using System.Collections.Generic;
using MediaForce.Client.Common;
using MediaForce.Client.OutlookHelper.Dal;
using MediaForce.Client.Common.Interfaces;
using System.Data;

namespace MediaForce.Client.OutlookHelper
{
    public class ClientSettingsService
    {
        //public static SyncItem SaveSyncItem(SyncItem syncItem)
        //{
        //    ClientSettingsDao.SyncItemDelete(syncItem.EntryId);
        //    ClientSettingsDao.SyncItemInsert(syncItem);
        //    return syncItem; // DataMappingUtility.FromDataRow<SyncItem>(dt.Rows[0]);
        //}

        //public static List<SyncItem> GetSyncItems(string entryId, SyncItemType? syncItemType)
        //{
        //    List<SyncItem> items = new List<SyncItem>();
        //    DataTable dt = ClientSettingsDao.SyncItemSelect(entryId, syncItemType);
        //    
        //    foreach (DataRow row in dt.Rows)
        //    {
        //        items.Add(DataMappingUtility.FromDataRow<SyncItem>(dt.Rows[0]));
        //    }

        //    return items;
        //}

        //public static void DeleteSyncItem(SyncItem syncItem)
        {
            ClientSettingsDao.SyncItemDelete(syncItem.EntryId);
        }

        //public static string Login(string email, string password)
        //{
        //    Logger.DebugFormat("Login", "{0} {1}", email, password);
        //    string baseAddress = "http://localhost/MediaForce/WebServices/SecurityServiceProxy.asmx";
        //    BasicHttpBinding binding = new BasicHttpBinding();
        //    binding.AllowCookies = true;
        //    SecurityServiceProxySoap proxy =
        //        new SecurityServiceProxySoapClient(binding,
        //            new EndpointAddress(baseAddress)).ChannelFactory.CreateChannel(new EndpointAddress(baseAddress));
            
        //    string result = proxy.AuthenticateUser(email, password);
        //    try
        //    {
                
        //        Logger.InfoFormat("Login", "login successful");
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.FatalFormat("Login","login failed:{0}", ex);
        //        throw new Exception(ex.ToString());
        //    }
            
        //    return result;
        //}

        public static UserSetting GetUserSettings()
        {
            DataTable dt = ClientSettingsDao.ClientSettingsSelect();

            if (dt.Rows.Count > 0)
                return DataMappingUtility.FromDataRow<UserSettings>(dt.Rows[0]);

            return new UserSettings();
        }

        public static UserSettings SaveUserSettings(UserSettings UserSettings)
        {
            ClientSettingsDao.ClientSettingsDelete(UserSettings.EMailAddress);
            string password = UserSettings.Password;// Helpers.GetSha1(UserSettings.Password);
            ClientSettingsDao.ClientSettingsInsert( UserSettings.EMailAddress, password);
            return UserSettings;
        }
    }
}
